package com.b2s.scrumblr.kb.controller;

import com.b2s.scrumblr.kb.model.RequestResults;
import com.b2s.scrumblr.kb.model.User;
import com.b2s.scrumblr.kb.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created on 2016/9/1.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String index(){
        return "/user/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public RequestResults add(User user){
        try {
            userService.add(user);
        }catch (Exception e){
            LOGGER.error("add user info fatal:", e);
            return new RequestResults(false, "Add the user fatal!");
        }
        return new RequestResults(true, "Add the user success!");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public RequestResults update(User user){
        try {
            userService.updateUser(user);
        }catch (Exception e){
            LOGGER.error("update user info fatal:", e);
            return new RequestResults(false, "Update the user fatal!");
        }
        return new RequestResults(true, "Update the user success!");
    }
}
